var Component = {
	loadData:function(xmlNode, obj){
		if(obj==null || obj==undefined)
			obj = this.findComponent(xmlNode);
			
		var displayed = xmlNode.selectNodes('./visible');
		if(displayed.length > 0){
			displayed = displayed[0];
			if(displayed.text=='0'){
				obj.style.visibility = 'hidden';
			}else{
				obj.style.visibility = 'inherit';
			}
		}
		
		obj.al_hasInited = true;
	},
	
	findComponent:function(xmlNode){
		var name = xmlNode.getAttributeNode('id').nodeValue;
		var comp = $$(name);
		this.assertComponent(comp, name);
		return comp;
	},
	
	assertComponent:function(obj, name){
		if(obj==null || obj==undefined){
			alert('can not find component:' + name);
		}
	},
	
	hasInited:function(obj){
		return obj.al_hasInited;
	}
};

var Label = {
	loadData:function(xmlNode, label){
		if(label == null)
			label = Component.findComponent(xmlNode);
			
		Component.loadData(xmlNode, label);
		label.innerText = xmlNode.selectNodes('./text')[0].text;
	}
};

var HtmlText = {
	loadData:function(xmlNode, htmlText){
		if(htmlText == null)
			htmlText = Component.findComponent(xmlNode);
			
		Component.loadData(xmlNode, htmlText);
		htmlText.innerHTML = xmlNode.selectNodes('./c')[0].text;
	}
};

//可输入的文本框。
var TextInput = {
	loadData:function(xmlNode, text){
		if(text == null)
			text = Component.findComponent(xmlNode);
			
		Component.loadData(xmlNode, text);
		text.value = xmlNode.selectNodes('./text')[0].text;
	}
}

var Link = {
	loadData:function(xmlNode, link){
		if(link == null)
			link = Component.findComponent(xmlNode);
			
		Component.loadData(xmlNode, link);
		var t = xmlNode.selectNodes('./t')[0].text;
		link.innerText = t ? t : "";
		var h = xmlNode.selectNodes('./h')[0].text;
		link.href = h ? h : "#";
	}
};

var HTMLImage = {
	loadData:function(xmlNode, img){
		if(img == null)
			img = Component.findComponent(xmlNode);
			
		Component.loadData(xmlNode, img);
		img.src = xmlNode.selectNodes('./s')[0].text;
	}
};


var Tree = {
	loadData:function(xmlNode){
		var id = xmlNode.getAttribute('id');
		this._createTree($$(id), xmlNode.selectNodes('./t')[0].childNodes);
		Component.loadData(xmlNode, $$(id));
	},

	_createTree: function(tree, nodes){
		var strHtml = '<ul id="alpha_tree" style="font:bold">';
		for(var i=0, n=nodes.length; i<n; ++i){
			strHtml += this._createNodeHtml(nodes[i]);
		}
		strHtml += '</ul>';
		tree.innerHTML = strHtml;
		
		this.initTree(tree);
		if(!isIE()){
			tree.style.MozUserSelect = 'none';
		}
	},
	
	_createNodeHtml: function(node){
		var str = '<li><span t_t="t" UNSELECTABLE="on" value="'+node.getAttributeNode('v').nodeValue+'">';
		str += node.getAttributeNode('t').nodeValue + '</span>';
		if(node.childNodes.length > 0){
			str += '<ul>';
			for(var i=0,n=node.childNodes.length; i<n; ++i){
				str += this._createNodeHtml(node.childNodes[i]);
			}
			str += '</ul>';
		}
		str += '</li>';
		
		return str;
	},
	
	initTree: function(tree){
		var liChildren = tree.getElementsByTagName("li");
		for(var i=0, n=liChildren.length; i<n; ++i){
			var ulChildren = liChildren[i].getElementsByTagName("ul");

			var node = findElementsBy(liChildren[i], "t_t", "t")[0];
			node.style.cursor = 'pointer';
			bindEvent(node, "onclick", function(event){
				var t ;
				if(!event){
					t = window.event.srcElement;
				}else{
					t = event.target;
				}
				var ln = tree.lastClickNode;
				//if(ln == t)return;
				if(ln){
					ln.style.background = ln.bgValue;
					ln.style.color = ln.colorValue;
				}
				tree.lastClickNode = t;
				t.bgValue = t.style.background;
				t.colorValue = t.style.color;
				t.style.background = "highlight";
				t.style.color = "white";

				if(tree.getAttribute('onNodeSelected')){
					eval(tree.getAttribute('onNodeSelected')+'(t)');
				}
			});

			if(ulChildren.length > 0){
				bindEvent(node, "ondblclick", function(event){
					if(!event){
						Tree.showhide(window.event.srcElement.parentNode);
					}else{
						Tree.showhide(event.target.parentNode);
					}
				});

				var t = document.createElement("span");
				t.className = "alpha_tree_symbols";
				t.style.backgroundImage	="url("+getLibURI()+"default/image/treenode_expand_plus.gif)";
				bindEvent(t, "onclick", function(){Tree.showhide(this.parentNode);});
				//t.onclick = function(){Tree.showhide(this.parentNode);}
				liChildren[i].insertBefore(t, liChildren[i].firstChild);
				ulChildren[0].style.display = "none";
			}else{
				var t = document.createElement("span");
				t.className	= "alpha_tree_symbols";
				//t.style.backgroundImage	= "url("+getLibURI()+"default/image/node.png)";
				node.parentNode.insertBefore(t, node);
			}
		}
	},
	
	showhide: function(el){
		el.getElementsByTagName("ul")[0].style.display=(el.getElementsByTagName("ul")[0].style.display=="block")?"none":"block";

		el.getElementsByTagName("span")[0].style.backgroundImage=(el.getElementsByTagName("ul")[0].style.display=="block")?"url("+getLibURI()+"default/image/treenode_expand_minus.gif)":"url("+getLibURI()+"default/image/treenode_expand_plus.gif)";
	}
};

var Table = {
	loadData:function(xmlNode, table){
		if(table == null)
			table = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, table);
		
		$A(table, "al_init", 1);

		var dd = xmlNode.selectSingleNode('./data');
		var data = (dd==null) ? [] : dd.childNodes;
		var rowCount = (dd==null) ? 0 : parseInt(dd.getAttributeNode('rowCount').nodeValue); //总行数，不是当页行数。
		var rows = table.rows;
		var template_row;
		//先将上次由模板生成的行删掉。
		if(table.template_row){
			if(rows.length == 0){
				table.insertRow(-1);
				rows[0].parentNode.insertBefore(table.template_row, rows[0]);
				table.deleteRow(-1);
			}else{
				rows[0].parentNode.insertBefore(table.template_row, rows[table.template_row.idx]);
			}
		}
		for(var i=0; i<rows.length; ++i){
			var row = rows[i];
			if($A(row,'template') != null){
				if(!template_row){
					template_row = row;
					table.template_row = row;
					table.template_row.idx = i;
				}else{
					row.parentNode.removeChild(row);
					--i;
				}
			}
		}

		//根据模板生成新的行。
		$A(table, "al_lastRow", 0);
		//$A(table, "al_mouseover_lastRow", 0);
		$A(table, "al_selRows", []);
		var template_index = 0;
		var rowClass = $A(template_row, "rowClass") ? $A(template_row, "rowClass").split(";") : null;
		for(var i=0; template_index < data.length; ++template_index, ++i){
			var new_row = template_row.cloneNode(true);
			template_row.parentNode.insertBefore(new_row, template_row);
			//装载数据
			this._setRowData(new_row, data[template_index].childNodes);
			//初始化默认属性。
			$A(new_row, "al_table_id", table.id);
			$A(new_row, "al_ori_bgcolor", getStyle(new_row, "background-color"));
			$A(new_row, "al_clk_bgcolor", "");
			$A(new_row, "al_ori_class", "");
			$A(new_row, "al_clk_class", "");
			if(!getStyle(new_row, "cursor"))
				setStyle(new_row, "cursor", "pointer");
			//设置样式表class
			if(rowClass && rowClass.length > 0){
				var clazz = rowClass[i % rowClass.length];
				setClassName(new_row, clazz);
				$A(new_row, "al_ori_class", clazz);
			}
			
			//事件注册。
			//鼠标单击事件。
			bindEvent(new_row, "onclick", function(){
				Table.selectRow(this, null, window.event.ctrlKey);
				//暂时不支持shift键。
				/*var tbl = $$($A(this, "al_table_id"));
				var lastRow = tbl.rows[$A(tbl, "al_lastRow")];
				setStyle(lastRow, "background-color", $A(lastRow, "al_ori_bgcolor"));
				setStyle(this, "background-color", "#DAE1F3");
				var rowClick = $A(this, "rowClick");
				if(rowClick){
					window[rowClick](lastRow, this);
				}
				$A(this, "al_clk_bgcolor", getCurrentStyle(this, "background-color"));
				$A(lastRow, "al_clk_bgcolor", "");
				$A(tbl, "al_lastRow", this.rowIndex);//*/
			});
			//右键事件
			bindEvent(new_row, "oncontextmenu", function(){
				Table.selectRow(this, null, window.event.ctrlKey);
			})
			//鼠标双击事件
			bindEvent(new_row, "ondblclick", function(){
				var rowDblClick = $A(this, "rowDblClick");
				if(rowDblClick){
					if(window[rowDblClick])
						window[rowDblClick](this);
					else
						showDebugInfo("无法找到双击事件响应函数 "+rowDblClick);
				}
			});
			//mouseover事件
			bindEvent(new_row, "onmouseover", function(){
				var tbl = $$($A(this, "al_table_id"));
				/*var lastRow = tbl.rows[$A(tbl, "al_mouseover_lastRow")];
				setStyle(lastRow, "background-color", $A(lastRow, "al_clk_bgcolor"));
				if($A(lastRow, "al_clk_class"))
					setClassName(lastRow, $A(lastRow, "al_clk_class"));
				else
					setClassName(lastRow, $A(lastRow, "al_ori_class"));//*/
				var clazz = $A(this, "mouseoverClass");
				if(clazz)setClassName(this, clazz);
				if(getStyle(this, "background-color"))
					setStyle(this, "background-color", "");
				var rowMouseOver = $A(this, "rowMouseOver");
				if(rowMouseOver){ //触发用户注册事件。
					if(window[rowMouseOver])
						window[rowMouseOver](this);
					else
						showDebugInfo("无法找到mouseover事件响应函数 "+rowMouseOver);
				}
				//$A(tbl, "al_mouseover_lastRow", this.rowIndex);
			});
			//mouseout事件
			bindEvent(new_row, "onmouseout", function(){
				setStyle(this, "background-color", $A(this, "al_clk_bgcolor"));
				if($A(this, "al_clk_class"))
					setClassName(this, $A(this, "al_clk_class"));
				else
					setClassName(this, $A(this, "al_ori_class"));
					
				var rowMouseOut = $A(this, "rowMouseOut");
				if(rowMouseOut){ //触发用户注册事件。
					if(window[rowMouseOut])
						window[rowMouseOut](this);
					else
						showDebugInfo("无法找到mouseout事件响应函数 "+rowMouseOut);
				}
			});
			
			
			//template_row = new_row;
		}
		table.template_row.parentNode.removeChild(table.template_row);
		
		//设置分页栏
		if($A(table, 'pagingBar')){
			if(!PagingBar.isRegistered($A(table, 'pagingBar'))){
				$$($A(table, 'pagingBar')).rowCount = rowCount;
				PagingBar.register($A(table, 'pagingBar'));
			}else{
				PagingBar.setRowCount($A(table, 'pagingBar'), rowCount);
			}
		}
		
		//if(data.length == 0)
		//	alert("结果为空。");
	},
	
	//创建一个table对象。
	createInstance:function(tableId){
		return new function(){
			var tid = $$(tableId).id;
			
			this.selectRow = function(row, isAdd){
				Table.selectRow(row, tid, isAdd);
			}
			this.unSelectRow = function(row){
				Table.unSelectRow(row, tid);
			}
			this.getRow = function(row){
				return Table.getRow(row, tid)
			}
			this.deleteRow = function(row){
				Table.deleteRow(row, tid);
			}
			this.getSelectedRows = function(){
				return Table.getSelectedRows(tid);
			}
			this.getItem = function(cell, row){
				return Table.getItem(tid, cell, row);
			}
			this.setItem = function(value, cell, row){
				Table.setItem(tid, value, cell, row);
			}
			this.getCurRowCount = function(){
				return Table.getCurRowCount(tid);
			}
		}
	},
	
	//row:row对象本身或rowIndex或row id，但如果是rowIndex的话必须传入tableId，其它两种则不必。
	//isAdd:是否增加选中行。true则在选择行列表中增加本行，false则只选中本行。默认为false。
	//调用这个方法会触发rowSelected事件。
	selectRow:function(row, tableId, isAdd){
		var rt = Table._getRowAndTable(row, tableId);
		row = rt[0];
		if(row == null)return;
		var tbl = rt[1];
		if(!$A(tbl, "al_init"))return;
		var rowIndex = row.rowIndex;
		var rows = tbl.rows;
		//加入选中行列表。
		var selRows = $A(tbl, "al_selRows");
		if(!isAdd){
			for(var i=0,n=selRows.length; i<n; ++i){
				Table._resetRowStyle(rows[selRows.pop()]);
			}
		}
		var isContain = false;
		for(var i=0,n=selRows.length; i<n; ++i){
			if(selRows[i] == rowIndex){
				isContain = true;
				break;
			}
		}
		if(!isContain)selRows.push(rowIndex);
		//设置样式，触发事件。
		var lastRow = rows[$A(tbl, "al_lastRow")];
		if(lastRow && !isAdd){
			Table._resetRowStyle(lastRow);
		}
		if($A(row, "selectedClass")){
			setClassName(row, $A(row, "selectedClass"));
			$A(row, "al_clk_class", $A(row, "selectedClass"));
		}else{
			setStyle(row, "background-color", "#DAE1F3");
		}

		var rowClick = $A(row, "rowSelected");
		if(rowClick){
			if(window[rowClick])
				window[rowClick](row, lastRow);
			else 
				showDebugInfo("无法找到单击事件响应函数 "+rowClick);
		}

		$A(row, "al_clk_bgcolor", getStyle(row, "background-color"));
		$A(tbl, "al_lastRow", rowIndex);
	},
	
	//取消选择。
	unSelectRow:function(row, tableId){
		var rt = Table._getRowAndTable(row, tableId);
		row = rt[0];
		if(row == null)return;
		var tbl = rt[1];
		if(!$A(tbl, "al_init"))return;
		var rowIndex = row.rowIndex;
		var rows = tbl.rows;
		var selRows = $A(tbl, "al_selRows");
		for(var i=0,n=selRows.length; i<n; ++i){
			if(selRows[i] == rowIndex){
				selRows.splice(i, 1);
				break;
			}
		}
		Table._resetRowStyle(row);
		$A(tbl, "al_lastRow", -1);
	},
	
	getRow:function(row, tableId){
		var rt = Table._getRowAndTable(row, tableId);
		return rt[0];
	},
	
	deleteRow:function(row, tableId){
		removeElement(Table._getRowAndTable(row, tableId)[0]);
	},
	
	//返回当前所有选中的行索引。
	getSelectedRows:function(tableId){
		return $A(tableId, "al_selRows");
	},
	
	//cell:cell对象本身或cellIndex或cell id
	//row:row对象本身或rowIndex或row id,如果不传入表示当前选中行。
	getItem:function(tableId, cell, row){
		cell = Table._getCell(cell, row, tableId);
		if(!cell)return null;
		var fChild = getChildren(cell)[0];
		if(fChild){
			if($A(fChild, "value") != null){
				return $A(fChild, "value");
			}
			switch(fChild.nodeName){
				case "IMG" : 
					return fChild.src;
				default:
					return fChild.innerText;
				
			}
		}else{
			return cell.innerText;
		}
	},
	
	setItem:function(tableId, value, cell, row){
		cell = Table._getCell(cell, row, tableId);
		if(!cell)return;
		var fChild = getChildren(cell)[0];
		if(fChild){
			if($A(fChild, "value") != null){
				$A(fChild, "value", value);
			}
			switch(fChild.nodeName){
				case "IMG" : 
					fChild.src = value;
				default:
					fChild.innerHTML = value;
				
			}
		}else{
			cell.innerHTML = value;
		}
	},
	
	//获取当前页的行数。
	getCurRowCount:function(tableId){
		if(!Component.hasInited($$(tableId)))return 0;
		return $$(tableId).rows.length;
	},
	
	_resetRowStyle:function(row){
		setStyle(row, "background-color", $A(row, "al_ori_bgcolor"));
		setClassName(row, $A(row, "al_ori_class"));
		$A(row, "al_clk_bgcolor", "");
		$A(row, "al_clk_class", "");
	},
	
	//row:row对象本身或rowIndex或row id
	_getRowAndTable:function(row, tableId){
		var tbl;
		if(typeof row == 'number'){	//rowIndex
			tbl = $$(tableId);
			row = tbl.rows[row];
		}else{
			row = $$(row);
			var tbl = $$($A(row, "al_table_id"));
		}
		return [row, tbl];
	},
	
	_getCell:function(cell, row, tableId){
		if(row == null){
			var selRows = Table.getSelectedRows(tableId);
			row = selRows[selRows.length-1];
		}
		if(row == null)return null;
		row = Table._getRowAndTable(row, tableId)[0];
		if(typeof cell == 'number'){ //索引
			return row.cells[cell];
		}else{
			return $$(cell);
		}
	},
	
	
	_setRowData:function(row, colsXml){
		if(row.cells.length != colsXml.length)
			throw ("表格的列数与传入的数据的列数不符。表格列数："+row.cells.length+" 数据列数："+colsXml.length);
		var col = row.firstChild;
		for(var c=0; c<colsXml.length;){
			if(col.nodeName.charAt(0) == '#' || $A(col,'ori') != null){
				col = col.nextSibling;
				continue;
			}
			if(colsXml[c].tagName == 's'){
				if(col.valueMapping != null){
					eval("var map="+col.valueMapping);
					var t = map[colsXml[c].text];
					col.innerText = (t==null)?' ':t;
				}else{
					col.innerText = colsXml[c].text=='' ? ' ' : colsXml[c].text;
				}
			}else if(colsXml[c].tagName == 'c'){
				var nodes = colsXml[c].childNodes;
				for(var i=0; i<nodes.length; ++i){
					eval(nodes[i].tagName).loadData(nodes[i], findElementsBy(col, "cid", nodes[i].getAttributeNode('id').nodeValue)[0]);
				}
			}else{
				eval(colsXml[c].tagName).loadData(colsXml[c], col.firstChild);
			}
			++c;
			col = col.nextSibling;
		}
	}
};

var Grid = {
	loadData:function(xmlNode, table){
		Table.loadData(xmlNode, table);
	}
};

var Select = {
	loadData:function(xmlNode, select){
		if(select == null)
			select = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, select);
		
		select.innerHTML = '';
		var optionsXml = xmlNode.selectNodes('./options')[0].childNodes;
		for(var i=0; i<optionsXml.length; ++i){
			var o = document.createElement("OPTION");
			o.value = optionsXml[i].getAttributeNode('v').nodeValue;
			o.text = optionsXml[i].text;
			select.options.add(o);
			if(optionsXml[i].getAttributeNode('s'))
				o.selected = true;
		}
	},
	
	//Select.setValue(id, ["abc", "1"]);
	setValue:function(id, values){
		var s = $$(id);
		var c = s.children;
		for(var i=0; i<c.length; ++i){
			var selected = false;
			for(var j=0; j<values.length; ++j){
				if(c[i].value == values[j]){
					c[i].selected = selected = true;
					break;
				}
			}
			if(!selected)
				c[i].selected = false;
		}
	}
};

var CheckBox = {
	loadData:function(xmlNode, checkbox){
		if(checkbox == null)
			checkbox = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, checkbox);

		var cbData = xmlNode.selectSingleNode('./cb');
		var cb = document.createElement("INPUT");
		cb.type = "CHECKBOX";
		cb.value = cbData.getAttributeNode('v').nodeValue;
		if(cbData.getAttributeNode('c'))
			cb.checked = true;
		//if(checkbox.cid)
		//	cb.cid = checkbox.cid;
		$A(checkbox, "value", $A(cb, "value"));
			
		checkbox.innerHTML = cb.outerHTML + cbData.getAttributeNode('t').nodeValue;
	}
};

var CheckBoxGroup = {
	loadData:function(xmlNode, group){
		if(group == null)
			group = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, group);
		
		var boxs = findElementsBy(group, "type", "checkbox");
		var vals = xmlNode.selectSingleNode('./values').childNodes;
		for(var i=0; i<boxs.length; ++i){
			var j=0;
			for(; j<vals.length; ++j){
				if(boxs[i].value == vals[i].text){
					boxs[i].checked = true;
					break;
				}
			}
			if(j==vals.length)
				boxs[i].checked = false;
		}
		
		var cbs = xmlNode.selectSingleNode('./checkboxes').childNodes;
		for(var i=0; i<cbs.length; ++i){
			var cb = document.createElement("INPUT");
			cb.type = "CHECKBOX";
			cb.value = cbs[i].getAttributeNode('v').nodeValue;
			if(cbs[i].getAttributeNode('c'))
				cb.checked = true;
			//cb.outerHTML += cbs[i].getAttributeNode('t').nodeValue;
			$$(cbs[i].getAttributeNode('id').nodeValue).innerHTML = cb.outerHTML + cbs[i].getAttributeNode('t').nodeValue;
		}
	}
};

var Radio = {
	loadData:function(xmlNode, radio){
		if(radio == null)
			radio = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, radio);
		
		var r = document.createElement("INPUT");
		r.type = "RADIO";
		var xn = xmlNode.selectSingleNode('./r');
		
		$A(r, "NAME", xn.getAttributeNode('gid').nodeValue);
		$A(r, "value", xn.getAttributeNode('v').nodeValue);
		if(xn.getAttributeNode('c').nodeValue == '1')
			$A(r, "checked", true);
			
		radio.innerHTML = r.outerHTML + xn.getAttributeNode('t').nodeValue;
		$A(radio, "value", $A(r, "value"));
	}
};

var RadioGroup = {
	loadData:function(xmlNode, group){
		if(group == null)
			group = Component.findComponent(xmlNode);
		Component.loadData(xmlNode, group);
		
		var xn = xmlNode.selectSingleNode('./radios');
		var cbs = xn.childNodes;
		var value = xn.getAttributeNode('v').nodeValue;
		group.value = value;
		var groupName = xn.getAttributeNode('gid').nodeValue;
		for(var i=0; i<cbs.length; ++i){
			var cb = document.createElement("INPUT");
			cb.type = "RADIO";
			$A(cb, "NAME", groupName);
			cb.value = cbs[i].getAttributeNode('v').nodeValue;
			var c = $$(cbs[i].getAttributeNode('id').nodeValue);
			if(!c)
				c = findElementsBy(group, "cid", cbs[i].getAttributeNode('id').nodeValue)[0];
			c.innerHTML = cb.outerHTML + cbs[i].getAttributeNode('t').nodeValue;
		}
		var radios = findElementsBy(group, "type", "radio");
		for(var i=0; i<radios.length; ++i){
			if(radios[i].value == value){
				radios[i].checked = true;
				break;
			}
		}
	}
};

var Message = {
	loadData:function(xmlNode, messageBox){
		var page = xmlNode.selectSingleNode('./msgpage');
		document.open();
		document.write(page.text);
		document.close();
		$$("message").innerHTML = xmlNode.selectSingleNode('./msg').text;
	}
};


var PagingBar = {	
	objs:new Array(),
	
	doAction:function(bar, pageNo, condition){
		if(!PagingBar.isRegistered(bar))
			bar = this.register(bar);
		else
			bar = $$(bar);
		if(pageNo != null)
			bar.curPage = pageNo;
		//AjaxAction.syncCall(bar.action, bar.curPage, bar.pageSize);
		var tb = $$($A(bar,'table'));
		var tcond = $A(tb,'condition');
		var torder = $A(tb,'orderBy');
		if(condition || (tb && (tcond != undefined || torder != undefined))){
			if(!condition){
				if(typeof tcond == 'string' && tcond.length > 0){
					eval('condition='+tcond);
				}else if(tcond){
					condition = tcond;
				}else{
					condition = {};
				}
			}
			if(typeof torder == 'string' && torder.length>0){
				condition.orderBy = torder;
				condition.asc = $A(tb,'asc')?$A(tb,'asc'):'';
			}

			AjaxAction.syncCall($A(bar,'action'), bar.curPage, bar.pageSize, condition);
		}else{
			AjaxAction.syncCall($A(bar,'action'), bar.curPage, bar.pageSize);
		}
		
		Table.selectRow(0, $A(bar,'table'))
	},

	register:function(id){
		var bar = PagingBar.objs[id] = $$(id);
		bar.curPage = 1;
		bar.pageSize = parseInt($A(bar, 'pageSize'));
		if(!bar.rowCount)
			bar.rowCount = 0;
		if(!bar.barStyle)
			bar.barStyle = 1;
		if(!bar.pageSels)
			bar.pageSels = 10;
		bar.pageSels = parseInt($A(bar, 'pageSels'));
		this._show(id);
		return bar;
	},
	
	isRegistered:function(id){
		return PagingBar.objs[id] != null || ($$(id)!=null && PagingBar.objs[$$(id).id] != null);
	},

	setRowCount: function(id, count){
		var bar = this.objs[id];
		bar.rowCount = count;
		this._show(id);
	},

	_show:function(id){
		var bar = this.objs[id];
		eval('this._style'+bar.barStyle+'(bar)')
	},
	
	_style0:function(bar){
		var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;
		
		var rowC = findElementsBy(bar, "nav_attr", "rowCount");
		if(rowC.length>0){
			for(var i=0; i<rowC.length; ++i){
				rowC[i].innerText = bar.rowCount;
			}
		}
		var curP = findElementsBy(bar, "nav_attr", "curPage");
		if(curP.length>0){
			for(var i=0; i<rowC.length; ++i){
				curP[i].innerText = bar.curPage;
			}
		}
		var pC = findElementsBy(bar, "nav_attr", "pageCount");
		if(pC.length>0){
			for(var i=0; i<rowC.length; ++i){
				pC[i].innerText = pageCount;
			}
		}
		
		var fpage = findElementsBy(bar, "nav_attr", "first");
		if(fpage.length>0 && pageCount<=0) //首页失效。
			fpage[0].disabled = true;
		else if(fpage.length>0)
			fpage[0].disabled = false;
			
		var lpage = findElementsBy(bar, "nav_attr", "last");
		if(lpage.length>0 && pageCount<=0) //末页失效。
			lpage[0].disabled = true;
		else if(lpage.length>0)
			lpage[0].disabled = false;
			
		var ppage = findElementsBy(bar, "nav_attr", "pre");
		if(ppage.length>0 && (pageCount<=0 || bar.curPage<2)) //上一页失效。
			ppage[0].disabled = true;
		else if(ppage.length>0)
			ppage[0].disabled = false;
			
		var npage = findElementsBy(bar, "nav_attr", "next");
		if(npage.length>0 && bar.curPage>=pageCount) //下一页失效。
			npage[0].disabled = true;
		else if(npage.length>0)
			npage[0].disabled = false;
		
		var dpage = findElementsBy(bar, "nav_attr", "designated");
		if(dpage.length>0 && pageCount<=0) //指定页失效。
			dpage[0].disabled = true;
		else if(dpage.length>0)
			dpage[0].disabled = false;
			
		var number = findElementsBy(bar, "nav_attr", "number");
		var c_n = findElementsBy(bar, "nav_attr", "number_c");
		c_n = (c_n.length>0) ? c_n[0] : null;
		if(number.length > 0){
			bar.number_templ = number[0];
			for(var i=(c_n)? 0 : 1; i<number.length; ++i)
				number[i].parentNode.removeChild(number[i]);
		}
		if(c_n){
			c_n.innerText = bar.curPage;
		}
		if(bar.number_templ){
			var s = bar.curPage - 2;
			if(s < 1 || pageCount <= bar.pageSels)s=1;
			else if(s + bar.pageSels > pageCount)s = pageCount - bar.pageSels + 1;
			var e = s + bar.pageSels - 1;
			if(e > pageCount)e = pageCount;
			
			var endp = (c_n) ? c_n.nextSibling : bar.number_templ.nextSibling;
			for(var i=s; i<=e; ++i){
				var cl = bar.number_templ.cloneNode(true);
				cl.innerText = i;
				if(c_n){
					if(i < bar.curPage)
						c_n.parentNode.insertBefore(cl, c_n);
					else if(i > bar.curPage)
						endp.parentNode.insertBefore(cl, endp);
				}else{
					endp.parentNode.insertBefore(cl, endp);
				}
			}
			if(!c_n){
				if(s > e)
					bar.number_templ.innerText = s;
				else
					bar.number_templ.parentNode.removeChild(bar.number_templ);
			}
		}
	
		bindEvent(bar, 'onclick', function(event) {
			stopEvent(event);
			var nav_attr = event.srcElement.nav_attr;
			if(!$A(bar,'action') || bar.rowCount==0 || !nav_attr || event.srcElement.disabled
				|| bar.lastEvent == event)
				return;
			bar.lastEvent = event;
			
			var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;
			var act = event.srcElement.innerText;
			if(nav_attr == "number"){   
				var p = parseInt(act);
				if(isNaN(p) || p<1 || p>pageCount){
					return;
				}else{
					bar.curPage = p;
				}
			}else if(nav_attr == "first"){
				bar.curPage = 1;
			}else if(nav_attr == "pre"){
				--bar.curPage;
			}else if(nav_attr == "next"){
				++bar.curPage;
			}else if(nav_attr == "last"){
				bar.curPage = pageCount;
			}else if(nav_attr == "designated"){   
				var p = parseInt($$(event.srcElement.iid).value);
				if(isNaN(p) || p<1 || p>pageCount){
					alert('不存在该页');
					return;
				}else{
					bar.curPage = p;
				}
			}else{
				return;
			}
	
			PagingBar.doAction(bar);
			//翻页请求处理完毕。
			if(event.srcElement.onfinished)
				eval(event.srcElement.onfinished+'()');
			else if(window[bar.onfinished]){
				window[bar.onfinished]();
			}
		});
	},

	_style1:function(bar){
		bar.innerHTML = '';
		var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;

		var fbn = document.createElement('button');
		fbn.innerText = '首页';
		if(pageCount<=0)  //注意页号从1开始。
			fbn.disabled = true;
		bar.appendChild(fbn);

		var lp = document.createElement('button');
		lp.innerText = '上一页';
		if(pageCount<=0 || bar.curPage<2)  //注意页号从1开始。
			lp.disabled = true;
		bar.appendChild(lp);

		var np = document.createElement('button');
		np.innerText = '下一页';		
		if(bar.curPage>=pageCount)  //注意页号从1开始。
			np.disabled = true;
		bar.appendChild(np);

		var lbn = document.createElement('button');
		lbn.innerText = '末页';
		if(pageCount<=0)  //注意页号从1开始。
			lbn.disabled = true;
		bar.appendChild(lbn);

		var selP = document.createElement('button');
		selP.innerText = '指定页:';
		if(pageCount<=0)
			selP.disabled = true;
		bar.appendChild(selP);

		var tp = document.createElement('input');
		if(pageCount<=0){
			tp.disabled = true;
		}else{
			tp.value = bar.curPage;
		}
		tp.width = 30;
		bar.appendChild(tp);

		var msg = document.createElement('text');
		msg.innerText = '('+(pageCount<=0?0:bar.curPage)+'/'+(pageCount)+') 总行数：'+bar.rowCount;
		bar.appendChild(msg);

		bindEvent(bar, 'onclick', function(event) {
			stopEvent(event);
			if(!$A(bar,'action') || bar.rowCount==0 || event.srcElement.disabled || bar.lastEvent == event)return;
			bar.lastEvent = event;
			
			var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;
			var act = event.srcElement.innerText;
			if(act == '首页'){
				bar.curPage = 1;
			}else if(act == '上一页'){
				--bar.curPage;
			}else if(act == '下一页'){
				++bar.curPage;
			}else if(act == '末页'){
				bar.curPage = pageCount;
			}else if(act == '指定页:'){   
				var p = parseInt(event.srcElement.nextSibling.value);
				if(p<1 || p>pageCount){
					alert('不存在该页');
					return;
				}else{
					bar.curPage = p;
				}
			}else{
				return;
			}
			
			PagingBar.doAction(bar);
			//翻页请求处理完毕。
			if(event.srcElement.onfinished)
				eval(event.srcElement.onfinished+'()');
			else if(window[bar.onfinished]){
				window[bar.onfinished]();
			}
		});	
	},
	
	_style2:function(bar){
		bar.innerHTML = '';
		var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;
	
		var html = '<div class="p_bar"><a class="p_total">&nbsp;'+bar.rowCount+'&nbsp;</a><a class="p_pages">&nbsp;'+(pageCount==0?0:bar.curPage)+'/'+pageCount+'&nbsp;</a>';
	
		var s = bar.curPage - 2;
		if(s < 1 || pageCount <= bar.pageSels)s=1;
		else if(s + bar.pageSels > pageCount)s = pageCount - bar.pageSels + 1;
		var e = s + bar.pageSels - 1;
		if(e > pageCount)e = pageCount;
	
		if(s > 1){
			html += '<a class="p_redirect" href="#">|<</a>'
		}
		if(bar.curPage > 1){
			html += '<a class="p_redirect" href="#"><<</a>'
		}
	
		for(var i=s; i<=e; ++i){
			if(i == bar.curPage){
				html += '<a href="#" class="p_curpage">'+i+'</a>'
			}else{
				html += '<a href="#" class="p_num">'+i+'</a>'
			}
		}
	
		if(bar.curPage < pageCount){
			html += '<a class="p_redirect" href="#">>></a>'
		}
		if(e < pageCount){
			html += '<a class="p_redirect" href="#">>|</a>'
		}
	
		bar.innerHTML = html;
		bindEvent(bar, 'onclick', function(event) {
			stopEvent(event);
			var cName = event.srcElement.className;
			if(!$A(bar,'action') || bar.rowCount==0 || (cName != "p_num" && cName != "p_redirect") || bar.lastEvent == event)return;
			bar.lastEvent = event;
			
			var pageCount = bar.rowCount==0?0:Math.round((bar.rowCount-1)/bar.pageSize-0.5)+1;
			var act = event.srcElement.innerText;
			if(act == '|<'){
				bar.curPage = 1;
			}else if(act == '<<'){
				--bar.curPage;
			}else if(act == '>>'){
				++bar.curPage;
			}else if(act == '>|'){
				bar.curPage = pageCount;
			}else{   
				var p = parseInt(act);
				if(isNaN(p) || p<1 || p>pageCount){
					return;
				}else{
					bar.curPage = p;
				}
			}
	
			PagingBar.doAction(bar);
			//翻页请求处理完毕。
			if(event.srcElement.onfinished)
				eval(event.srcElement.onfinished+'()');
			else if(window[bar.onfinished]){
				window[bar.onfinished]();
			}
		});
	}
};


//------------------------------------------------
//cbMap:回调函数。{'save':function(){}, 'init':function(){}}
function HtmlEditor(oHolder, cbMap){
	this.oHolder = oHolder;
	this.editorId = "alpha_" + oHolder + "_" + new Date().getTime();
	if(cbMap){
		window[this.editorId+"_fnSave"] = cbMap["save"];	//加入保存处理函数。
		window[this.editorId+"_fnInit"] = cbMap["init"];
	}
	_loadHtml(getLibURI()+"FCKeditor/FCKeditor.jsp?id="+this.editorId, oHolder);
	
	this.getEditor = function(){
		return $$(this.oHolder).childNodes[0].contentWindow.FCKeditorAPI.GetInstance(this.editorId);
	}
	
	this.getHtml = function(){
		return this.getEditor().GetXHTML(true);
	};
	
	this.setHtml = function(strHtml){
		this.getEditor().SetHTML(strHtml);
	}
	
	this.insertHtml = function(strHtml){
		this.getEditor().InsertHtml(strHtml);
	}
}


//-------------------------------------

var ModalDialog = {
	isOpen:false,
	
	open:function(dialogId, title, width, height){
		loadSysJS("/private/jquery.MessageBox.js");
		var option = {};
		option.control = dialogId;
		option.title = title;
		option.width = width;
		option.height = height;
		ShowMessageBox(option);
		ModalDialog.isOpen = true;
	},
	
	close:function(){
		if(ModalDialog.isOpen){
			CloseMessageBox();
		}
	}
};

//----------------------------------------------------------
//blockedUI:需要弹出模态框的区域，如果需要整个页面则可传入window
function BlockUI(blockedUI, content, style){
	loadSysJS("/private/jquery.blockUI.js");
	
	this.open = function(){
		if(blockedUI == window){
			$.blockUI(content, style); 
		}else{
			$($$(blockedUI)).block(content, style);
		}
	}
	
	this.close = function(){
		if(blockedUI == window){
			$.unblockUI();
		}else{
			$($$(blockedUI)).unblock();
		}
		
		if(typeof content == 'object'){//html对象
			content.style.display = 'none';
			document.body.appendChild(content);
		}
	}
}

/**
 * 右键菜单。
 * @param {Object} obj : 要设置邮件菜单的对象。
 * @param {Object} menuId ： 菜单html的id。
 * @param {Object} menuAttr ：菜单的属性描述。
 */
function contextMenu(obj, menuId, menuAttr){
	loadSysJS("/private/jquery.contextmenu.js");
	$($$(obj)).contextMenu(menuId, menuAttr);
}


//弹出对话框。
//注意：这个方法管理着一个全局唯一的窗口，不能用于多窗口的弹出。
//sUrl:窗口所对应的页面。
//sArgs:url后面的参数。
//sDialogWidth：窗口宽度。
//sDialogHeight：窗口高度。
//sResizable：是否可拖拉改变大小。目前无效。
//sCNTitle:中文标题。
//sENTitle：英文标题。目前无效。
function OpenDivDialog(sUrl,sArgs,sDialogWidth,sDialogHeight,sResizable,sCNTitle,sENTitle)
{
	if(alp_DivDialog.url==sUrl)return;
	closeDivDialog();
	alp_DivDialog.url=sUrl;
	loadSysJS("private/jquery.easydrag.js");
	var dlg_name = alp_DivDialog.id;
	var dlg_title_name = "OpenDivDialog_title_"+new Date().getTime();
	var width = parseInt(sDialogWidth);
	var height = parseInt(sDialogHeight);
	var left = (document.body.clientWidth-width)/2;
	var top = (document.body.clientHeight-height)/2;
	if(!sCNTitle)sCNTitle="";
	if(sUrl.indexOf("?") > -1)
		sUrl += "&";
	else
		sUrl += "?";
		
	var strdlg = '<div id="'+dlg_name+'" style="width:'+width+'px;height:'+height+'px;position:absolute;left:'+left+'px;top:'+top+'px;">'+
			'<div id="'+dlg_title_name+'" style="width:100%;border:1px solid #01a1de;">' +
				'<TABLE width="100%" height="20px" border="0" CELLPADDING="0" CELLSPACING="0">' + 
				'<TR>' + 
					'<TD style="background-image:url(/Global/default/image/head.gif);font-size:13px;COLOR:#eaf0f7;"><div style="white-space:nowrap;text-overflow:ellipsis;overflow-x:hidden;width:'+(width-30)+'px;cursor:move;">&nbsp;'+sCNTitle+'</div></TD>' + 
					'<TD style="background-image:url(/Global/default/image/closeIMG1.gif);width:20px;" onclick="closeDivDialog()"></TD>' + 
				'</TR>' +
				'</TABLE>' + 
			'</div>' +
			'<div style="border:1px solid #01a1de;width:100%;height:'+height+'px;">' + 
				'<IFRAME style="WIDTH: 100%; HEIGHT: 100%" frameBorder="0" src="'+sUrl+sArgs+'"></IFRAME>' +
			'</div>' +
			(isIE()?'<iframe src="javascript:false" style="position:absolute; visibility:inherit; top:0px; left:0px; width:100%; height:20px; z-index:-1; filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe>' : "") +
		'</div>'
		
	$(document.body).append(strdlg);
	$('#'+dlg_name).easydrag().ondrop(function(){
		if($$(alp_DivDialog.id).offsetLeft < 0)
			setStyle(alp_DivDialog.id, "left", "0");
		if($$(alp_DivDialog.id).offsetTop < 0)
			setStyle(alp_DivDialog.id, "top", "0");
	 });;
}
var alp_DivDialog = {id:"OpenDivDialog_1192677412160", url:""};
function closeDivDialog(isRefesh){
	if(isRefesh==null) isRefesh = false;
	removeElement(alp_DivDialog.id);
	alp_DivDialog.url = "";
	if(isRefesh)
		window.location.reload();
}

//-----------------------------------------------------------------------------------